<?php
namespace api\admin\controller;

use api\admin\model\FeedbackModel;
use api\admin\model\FeedbackRecordModel;
use api\admin\model\FeedbackTagModel;
use api\admin\model\ToiletModel;
use api\user\controller\PublicController;
use cmf\controller\RestBaseController;
use think\Db;

class FeedBackController extends RestBaseController
{

    protected $beforeActionList = [
        'getType',
        // 'toiletcheck' => ['only' => 'addtoilet'],
    ];

    /**
     * [getType 前置操作]
     * @Author   HUI
     * @DateTime 2018-11-26
     * @version  [version]
     * @param    string     $value [description]
     */
    public function getType($value = '')
    {
        if ($this->getUserType() !== 3)
        {
            $this->error('暂无创建权限');
        }
    }

    /**
     * [FeedBackList 建议列表]
     */
    public function feedBackList()
    {
        $feedback = new FeedbackModel();
        $list = $feedback->getFeedBackList($this->getUserId());
        return $this->success('获取成功', $list);

    }

    /**
     * [getFeedBack 获取当前反馈详情]
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function getFeedBack($value = '')
    {
        $param = $this->request->param();
        //为空判断
        if (empty($param['id']))
        {
            $this->error('参数有误！');
        }
        $feedback = new FeedbackModel();
        $detail = $feedback->getFeedBackDetail($param['id']);
        $this->success('获取成功',$detail);
        
    }

    /**
     * [delFeedBack 删除公厕反馈]
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function delFeedBack()
    {
        $param = $this->request->param();
        //为空判断
        if (empty($param['id']))
        {
            $this->error('参数有误！');
        }
        $feedback = new FeedbackModel();
        // print_r($param);exit();
        $result = $feedback->where('id',$param['id'])->update(['status'=>0]);
        if ($result) {

            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }

    }

    /**
     * [addFeedBackList 添加投诉选项/附加信息]
     */

    public function addFeedBack()
    {

        $param = $this->request->param();

        // print_r($param);exit();
        //为空判断
        if (empty($param['thumbnail']) || empty($param['tag']))
        {
            $this->error('参数有误！');
        }

        if (count($param['tag']) > 6) {
            $this->error('投诉建议不能超过6个');
        }

        foreach ($param['tag'] as $key => $value) {
            if (mb_strlen($value) >  4) {
                $this->error('请输入小于4个字符的投诉建议');
            }
        }


        $feedback = new FeedbackModel();
        $tag      = new FeedbackTagModel();
        $publics  = new PublicController();
        // $Toilet = new ToiletModel();

        //查询是否存在
        // $exist = $feedback->where('user_id',$this->getUserId())->find();
        // if ($exist) {
        //     //修改
        //     $this->error('不可重复添加');
        // }



        Db::startTrans();
        try {

            //
            $tag->dataInsertTag($param['tag'], $this->getUserId());
            //添加轮播图及电话
            // $Toilet->field('id')->where('user_id',$this->getUserId())->select()->toArray();


            $feedback->dataInsertFeedback([
                'thumbnail' => json_encode($param['thumbnail']),
                'telephone' => !empty($param['telephone']) ? $param['telephone'] : "",
            ],$this->getUserId());

            Db::commit();
        }
        catch (\Exception $e)
        {
            Db::rollback();
            $this->error('添加失败');
        }
        $this->success('添加成功');

    }

    /**
     * [editFeedBackList 编辑当前投诉选项]
     * @return [type] [description]
     */
    public function editFeedBack()
    {

        $param = $this->request->param();

        //为空判断
        if (empty($param['thumbnail']) || empty($param['t_id']) || empty($param['tag']) || empty($param['id']))
        {
            $this->error('参数有误！');
        }

        $feedback = new FeedbackModel();
        $tag      = new FeedbackTagModel();
        // $publics  = new PublicController();

        Db::startTrans();
        try {

            $feedback->where('id', $param['id'])->update([
                'thumbnail' => json_encode($param['thumbnail']),
                // 'feedback_code' => $publics->userCode(),
                't_id' => $param['t_id'],
                'telephone' => !empty($param['telephone']) ? $param['telephone'] : "",
                'create_time' => time(),
            ]);
            $tag->dataInsertTag($param['tag'], $param['id']);
            Db::commit();
        }
        catch (\Exception $e)
        {
            Db::rollback();
            $this->error('修改失败');
        }
        $this->success('修改成功');
    }

    /**
     * [getToiletList 模糊搜索当前客户下的公厕]
     * @return [type] [description]
     */
    public function searchToilet()
    {
        $param = $this->request->param();

        $where = [
            'user_id' => $this->getUserId(),
            'status' => 1,
        ];
        if (!empty($param['keywords']))
        {

            $where['title'] = ['like', "%{$param['keywords']}%"];
        }
        $Toilet = new ToiletModel();
        $list   = $Toilet->field('id,title')->where($where)->select();
        return $this->success('获取成功', ['list' => $list]);
    }

    //获取当前用户的tag配置
    public function getFeedBackTagDetail()
    {
        $where = [
            'user_id' => $this->getUserId(),
            'status' => 1,
        ];
        $tag      = new FeedbackTagModel();
        $feedback = new FeedbackModel();
        $tagArr = $tag->field('tag_name')->where($where)->select();
        $fee = $feedback->getFeedBackUserDetail($this->getUserId());
        $data  = [
            'tag' =>$tagArr,
            'telephone' =>$fee['telephone'],
            'thumbnail' => json_decode($fee['thumbnail'],true),
        ];
        return $this->success('获取成功', ['info' => $data]);
    }

    //
    public function getFeedBackCount()
    {   
        $param = $this->request->param();
        //
        $feedback = new FeedbackModel();
        $fee = $feedback->where('id',$param['id'])->find();

        //查询当前用户tag
        $tag = new FeedbackTagModel();
        $tagArr = $tag->where([
            'status'=>1,
            'user_id'=>$this->getUserId()
        ])->select();

        //
        $record = new FeedbackRecordModel();

        $tagRse = [];
        //查询
        foreach ($tagArr as $key => $value) {

            $count = $record->where([
                'tag_id' => $value['id'], 
                'satisfaction' => 0]
            )->count();

            $tagRse[$key] = [
                'name'=>$value['tag_name'],
                'value'=>$count,
            ]; 
        }


        $best = $record->where(['t_id' => $fee['t_id'], 'satisfaction' => 1])->count();
        $good = $record->where(['t_id' => $fee['t_id'], 'satisfaction' => 2])->count();
        $bad  = $record->where(['t_id' => $fee['t_id'], 'satisfaction' => 3])->count();


        $detail['tag']  = $tagRse;
        $detail['satisfaction']  = [
            [
                'value'=>$best,
                'name'=>'满意'
            ],
            [
                'value'=>$good,
                'name'=>'一般'
            ],
            [
                'value'=>$bad,
                'name'=>'不满意'
            ],
        ];

        return $this->success('获取成功', ['info' => $detail]);
        
    }



}
